package acom.test.swing.bb;

import java.awt.CardLayout;
import java.awt.Color;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ACardLayoutObserver implements Observer{
	
	static final int WIDTH = 600;
	static final int HEIGHT = 600;
	
	private JFrame frame;
	private JLabel title_lb;
	
	private JPanel mainPane;
	private JPanel cardPane;
	
	private CardLayout cardLayout ;
	
	private JPanel display_pane;
	private JPanel result_pane;
	
	private static int RESULT_PANE=2;
	private static int DISPLAY_PANE=1;
	
	public ACardLayoutObserver(){
		frame = new JFrame();
		mainPane = new JPanel();
		
		title_lb = new JLabel("设置");
		cardPane = new JPanel();
		cardLayout = new CardLayout();
		cardPane.setLayout(cardLayout);
		
		A1PaneTest panedisplay=new A1PaneTest();
		panedisplay.addObserver(this);
		
		A2PaneTest paneresult = new A2PaneTest();
		paneresult.addObserver(this);
		
		display_pane = panedisplay.getPane();
		result_pane = paneresult.getPane();
		
		//display_pane.add(new JButton("匿名"));
		
		cardPane.add(display_pane,"p1");
		cardPane.add(result_pane,"p2");
		
		init();
		
		mainPane.add(title_lb);
		mainPane.add(cardPane);
		frame.add(mainPane);
		frame.setSize(WIDTH, HEIGHT);
		frame.setVisible(true);
	}
	
	public void init(){
		//frame.setLayout(null);
				
		mainPane.setLayout(null);
		display_pane.setBackground(Color.BLUE);
		result_pane.setBackground(Color.RED);
		
		title_lb.setBounds(20,20,80,30);		
		cardPane.setBounds(20,50,300,200);
		cardPane.setBackground(Color.WHITE);
	}
	
	public static void main(String[] args) {
		new ACardLayoutObserver();

	}

	@Override
	public void update(Observable o, Object arg) {
		int paneKey = ((ObserverKey)o).getShowPane();
		
		System.out.println("paneKey:"+paneKey);
		
		if(paneKey==DISPLAY_PANE){
			cardLayout.show(cardPane, "p2");
		}else if(paneKey==RESULT_PANE){
			cardLayout.show(cardPane, "p1");
		}
	}

}
